<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>my博客</title>
    <link rel="stylesheet" href="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/css/bootstrap.css">

    <style>
        body{
            background-color: #f4f4f4;
        }
        .title{
            padding-top: 8px;
        }
        .content{
            margin-top: 40px;
            background-color: #ffffff;
        }
        .navbar-dark{
            background-color: #2C2827;
            line-height: 18px;
        }
        pre > code{
            background-color: #f4f4f4;
        }
    </style>
</head>
<body>
<div class="container">
    <nav class="navbar navbar-dark bg-faded">
        <a class="nav-brand title" href="#"><h2>Local</h2></a>
        <ul class="nav nav-nav">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Features</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Pricing</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">About</a>
            </li>
        </ul>
        <form class="form-inline nav-form pull-right">
            <input class="form-control" type="text" placeholder="Search">
            <button class="btn btn-success-outline" type="submit">Search</button>
        </form>
    </nav>

    <div class="content">
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
            <h1 class="post-title" itemprop="name headline"><a href="https://zxblog.our-dream.cn/index.php/archives/545.html" itemtype="url">【原创】如何配置Ajax的跨域请求</a></h1>
            <div class="post-meta">
                <span datetime="2017-02-28T11:09:00+08:00" itemprop="datePublished">February 28th , 2017</span>
            </div>
            <div class="post-content cf" itemprop="articleBody">
                <p>说道跨域的问题，由来已久，不是一个特别复杂的事情，但是从未认真的整理过相关文档，今年做个笔记吧！</p>
                <blockquote><p>配置服务器（Nginx）</p></blockquote>
                <p>在遇到跨域的情况的时候，客户端会先向服务器发送一个<code>options</code>的请求用以确认服务器是否允许跨域请求！</p>

                <pre><code>location / {
                    if ($request_method = 'OPTIONS') {
                    add_header Access-Control-Allow-Origin *;
                    add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
                    add_header Access-Control-Allow-Headers Content-Type,Content-Range,Content-Disposition,Content-Description;
                    return 204;
                    }
                    }
                </code></pre>
                <blockquote><p>修改代码（PHP）</p></blockquote>
                <p>再服务器允许跨域请求后，每次返回都得带上下面的头！</p>

                <pre>
                    <code>
                        header('Access-Control-Allow-Origin: *');
                        header('Access-Control-Allow-Credentials: true');
                        header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
                        header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
                    </code>
                </pre>


                <div class="post-copyright">
                    <div class="alert" role="alert">最后编辑时间为: February 28th , 2017 at 11:17 am<br>本文由 <a href="https://zxblog.our-dream.cn/index.php/author/1/">陌上花开</a> 创作，采用 <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/" rel="external nofollow">知识共享署名 4.0</a> 国际许可协议进行许可<br>可自由转载、引用，但需署名作者且注明文章出处</div>
                </div>
                <div class="post-tags"><a href="https://zxblog.our-dream.cn/index.php/tag/Ajax/">Ajax</a> <a href="https://zxblog.our-dream.cn/index.php/tag/%E8%B7%A8%E5%9F%9F/">跨域</a> <a href="https://zxblog.our-dream.cn/index.php/tag/CORS/">CORS</a></div>
            </div>
        </article>
    </div>
</div>
<script src="https://cdn.bootcss.com/jquery/1.12.3/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/js/bootstrap.js"></script>
</body>
</html>